﻿<?php
class comment extends top
{
	function index(){

		$data = $this->spArgs();

		if($data["submit"]){
			$insert_data = array(
					'module'=>'feedback',
					'module_id'=>0,
					'cat_id' => 46,
					'father_id'=>0,
					'user_id'=>0,
					'username'=>$data['username'],
					'title'=>$data['subject'],
					'phone'=>$data['email'],
					'content'=>$data['message'],
					'email'=>$data['email'],
				);

			$this->is_ok = spClass('m_comment')->create($insert_data);

			//require 'include/phpmailer/class.phpmailer.php';
			//$email_result = $this->sentmail($data['username'],$data['subject'],$data['message'],$data['email']);

		}

		//var_dump($this->result);
		$all_comment = array();
		foreach($this->result['comment'][0] as $key => $val){
			$temp_key = $key+1;
			$all_comment[$temp_key] = $val;
		}
		$this->all_comment = $all_comment;
	}

	function sentmail($username,$subject,$message,$contact_email){
		$server_host = $this->email_server_address;
 		$server_email = $this->email_server_username;
 		$server_password = $this->email_server_password;
 		$email = $this->email_recieve_address;

 		$server_name = '北木科技';
 		$email_subject = '有人在北木科技的官网留言了！';
 		$email_body = '<h1 style="font-size:18px;">主题：'.$subject.'</h1><p>姓名：'.$username.'</p><p>邮箱：'.$contact_email.'</p><p>内容：'.$message.'</p>';
		//require '/phpmailer/class.pop3.php'; 
		//require '/phpmailer/class.smtp.php';
		
		$mail                = new PHPMailer();
		$mail->CharSet = "UTF-8";
		$mail->Encoding = "base64";
		//$www = substr($_SERVER['SCRIPT_FILENAME'],0,-9);
		//$body                = file_get_contents($www.'/phpmailer/contents.html');
		//$body                = eregi_replace("[\]",'',$body);		
		$mail->IsSMTP(); // telling the class to use SMTP
		$mail->SMTPAuth      = true;                  // enable SMTP authentication
		$mail->SMTPKeepAlive = true;                  // SMTP connection will not close after each email sent
		$mail->Host          = $server_host; // sets the SMTP server
		$mail->Port          = 25;                    // set the SMTP port for the GMAIL server
		$mail->Username      = $server_email; // SMTP account username
		$mail->Password      = $server_password;        // SMTP account password
		$mail->SetFrom($server_email, $server_name);
		$mail->AddReplyTo($server_email, $server_name);

		$mail->Subject       = $email_subject;

		$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
		$mail->MsgHTML($email_body);
		$mail->AddAddress($email, $email);
		// $mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");

		if(!$mail->Send()) {
			return array($email,0,"Mailer Error (" . str_replace("@", "&#64;", $email) . ') ' . $mail->ErrorInfo . '<br />');
		} else {
			return array($email,1,"Message sent to :" . 'me' . ' (' . str_replace("@", "&#64;", $email) . ')<br />');
		}
		// Clear all addresses and attachments for next loop
		$mail->ClearAddresses();
		//$mail->ClearAttachments();		
	}
}